package com.example.comiccenter.service.implt;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.comiccenter.dao.CommentMapper;
import com.example.comiccenter.dao.UserMapper;
import com.example.comiccenter.domain.Comment;
import com.example.comiccenter.domain.User;
import com.example.comiccenter.service.CommentService;
import com.example.comiccenter.utils.ResultData;
import com.example.comiccenter.utils.SearchBean;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    @Resource
    private CommentMapper commentMapper;
    @Resource
    private UserMapper userMapper;

    @Override
    public ResultData deleteComment(Comment comment) {
        commentMapper.deleteById(comment.getId());
        QueryWrapper<Comment> commentQueryWrapper = new QueryWrapper<>();
        commentQueryWrapper.eq("parentId",comment.getId());
        List<Comment> comments = commentMapper.selectList(commentQueryWrapper);
        for (Comment comment1 : comments) {
            commentMapper.deleteById(comment1.getId());
        }
        return ResultData.success("删除成功");
    }

    //通用查询
    @Override
    public ResultData getAllComment(SearchBean<Comment> searchBean) {
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        wrapper
                .eq(searchBean.getData().getComicId()!=null,"comicId",searchBean.getData().getComicId())
                .eq(searchBean.getData().getUsername()!=null,"username",searchBean.getData().getUsername())
                .eq(searchBean.getData().getParentId()!=null,"parentId",searchBean.getData().getParentId())
                .orderBy(searchBean.getOrderField()!=null, searchBean.isAsc(), searchBean.getOrderField());

        Page<Comment> commentPage = commentMapper.selectPage(new Page<>(searchBean.getPageNum(), searchBean.getPageSize()), wrapper);
        return ResultData.success("查询成功",commentPage);
    }

    @Override
    public ResultData addComment(Comment comment) {
        commentMapper.insert(comment);
        return ResultData.success("评论成功");
    }

    @Override
    public ResultData getComicComment(int id,String column) {

        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        wrapper.eq("parentId",0).eq("comicId",id).orderBy(true,true,column);
        //一级评论
        List<Comment> comments = commentMapper.selectList(wrapper);

        List<HashMap<String, Object>> data = new ArrayList<>();
        for (Comment comment : comments) {
            HashMap<String, Object> commentMap = new HashMap<>();


            commentMap.put("comment",addAvatarToComment(comment));
            QueryWrapper<Comment> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("parentId",comment.getId()).eq("comicId",id).orderBy(true,true,column);
            List<Comment> comments1 = commentMapper.selectList(wrapper1);
            List<HashMap<String, Object>> hashMaps = new ArrayList<>();
            for (Comment comment1 : comments1) {
                hashMaps.add(addAvatarToComment(comment1));
            }
            commentMap.put("child",hashMaps);
            data.add(commentMap);
        }


        return ResultData.success("查询成功",data);
    }

    public HashMap<String, Object> addAvatarToComment(Comment comment){
        HashMap<String, Object> newComment = new HashMap<>();
        newComment.put("id",comment.getId());
        newComment.put("username",comment.getUsername());
        newComment.put("comicId",comment.getComicId());
        newComment.put("parentId",comment.getParentId());
        newComment.put("detail",comment.getDetail());
        newComment.put("likes",comment.getLikes());
        newComment.put("dislikes",comment.getDislikes());
        newComment.put("time",comment.getTime());
        newComment.put("replyId",comment.getReplyId());
        newComment.put("replyName",comment.getReplyName());
        newComment.put("name",comment.getName());
        QueryWrapper<User> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("username",comment.getUsername());
        newComment.put("avatar",userMapper.selectOne(wrapper2).getAvatar());
        return newComment;
    }

}
